package elearning.course.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import elearning.course.entity.Course;
import elearning.course.service.CourseService;


public class CourseAction extends ActionSupport implements ModelDriven<Course>{

	private Course course = new Course();
	private Integer uid;
	
	

	public void setUid(Integer uid) {
		this.uid = uid;
	}

	private CourseService courseService;	
	public void setCourseService(CourseService courseService) {
		this.courseService = courseService;
	}

	public Course getModel() {		
		return course;
	}

	public void list(){	
	}
	
	public void detail() throws IOException{
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		response.setCharacterEncoding("UTF-8");		
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();	
		String courseJson=null;
		try{
			if(uid==null) uid=0;
			System.out.println("userId = " +uid);		
			courseJson = courseService.findCourse(course.getCourseId(),uid);				
			System.out.println(courseJson);
			out.print(courseJson);
		}catch(Exception e){
			e.printStackTrace();
		}
		
			
	}
	
	 public void nearCourse() throws IOException{
		 HttpServletResponse response = ServletActionContext.getResponse();
		
		 response.setCharacterEncoding("UTF-8");		
		 response.setContentType("text/html;charset=utf-8");
		 PrintWriter out = response.getWriter();	
		 
			try{					
				String nearCourseJson = courseService.findNearCourse(uid);				
				out.print(nearCourseJson);				
			}catch(Exception e){
				e.printStackTrace();
			}
			
	 }
	
	 public void userCourse() throws IOException{
		 HttpServletResponse response = ServletActionContext.getResponse();
			
		 response.setCharacterEncoding("UTF-8");		
		 response.setContentType("text/html;charset=utf-8");
		 PrintWriter out = response.getWriter();
		 try{					
				String userCourse = courseService.finduserCourse(uid);				
				out.print(userCourse);				
			}catch(Exception e){
				e.printStackTrace();
			}
	 }
	
}
